import {http} from "../utils/http";
import {domain1} from "../utils/http"
//获取code
const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    });
  });
};

//获取openid
const getOpenid = (data) =>
  http({
    domain: 2,
    url: "openid.php",
    data: {
      ...data
    }
  })

//获取预支付的信息
const prepay = (data) =>
  http({
    domain: 2,
    url: "jsapi.php",
    method: "POST",
    data: {
      ...data
    }
  })

//调起支付 窗口
const payment = (data) => {
  return new Promise((resolve, reject) => {
    wx.requestPayment({ //窗口
      ...data,
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};
//轮播图
const blundList = (data) =>
  http({
    url: '/banner/list',
    method: "get",
    data: {
      ...data
    }
  })
//精选专题
const subjectList = (data) =>
  http({
    url: '/cms/news/list',
    method: "get",
    data: {
      ...data
    }
  })

//人气推荐
const personList = (data) =>
  http({
    url:"/shop/goods/list",
    method:"get",
    data: {
      ...data
    }
  })

//详情
const detailListss = ({id}) =>
  http({
    url:`/shop/goods/detail?id=${id}`,
    method:"post",
})

//分类
const cartList = () =>
  http({
    url:'/shop/goods/category/all',
    method:'post'
  })
const tuList = () =>
  http({
    url:'/shop/goods/category/all',
    method:'post'
  })

//图片验证码
const myImageUrl = () =>{
  let key = Date.now();
  return `${domain1}/verification/pic/get?key=${key}`
}

//获取短信验证码
const sms = ({mobile,picCode,key}) =>{
  return http({
    url:"/verification/sms/get",
    method:"get",
    header: {
      "content-type": "application/x-www-form-urlencoded"
    },
    data:{
      mobile,
      picCode,
      key,
    }
  })
}

//注册
const doRegister = ({mobile,pwd,code,nick}) =>{
  return http({
    url:"/user/m/register",
    header:{
      "content-type": "application/x-www-form-urlencoded"
    },
    method:"post",
    data:{
      mobile,
      pwd,
      code,
      nick,
    }
  })
}

//地址
const toAddress= (data) =>
  http({
    url: '/user/shipping-address/list',
    method: 'get',
    data: {
      ...data
    },
  })

// 删除地址
const delAddress = (data) =>{
return  http({
      url:"/user/shipping-address/delete",
      method:"get",
      data:{
        ...data
      }
    })
}
//添加地址
const addCity = (data) =>{
    return http({
      url:"/user/shipping-address/add",
      method:"POST",
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      data:{
        ...data
      }
    })
}

export {
  login,
  getOpenid,
  prepay,
  payment,
  blundList,
  subjectList,
  personList,
  detailListss,
  cartList,
  tuList,
  myImageUrl,
  sms,
  doRegister,
  toAddress,
  delAddress,
  addCity
}